package sv.com.action.comun;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;

public class ArchivoAction extends ComunAction {
	
	/**
	 * 
	 */
	
	private File upload;//The actual file
	private String uploadContentType; //The content type of the file
	private String uploadFileName; //The uploaded file name
	
	
	
	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	private static final long serialVersionUID = 1L;
	
	 /**
	  280        * Creates a RequestContext needed by Jakarta Commons Upload.
	  281        *
	  282        * @param req  the request.
	  283        * @return a new request context.
	  284        */
	         private RequestContext createRequestContext(final HttpServletRequest req) {
	             return new RequestContext() {
	                 public String getCharacterEncoding() {
	                     return req.getCharacterEncoding();
	                 }
	     
	                 public String getContentType() {
	                     return req.getContentType();
	                 }
	     
	                 public int getContentLength() {
	                     return req.getContentLength();
	                 }
	     
	                public InputStream getInputStream() throws IOException {
	                     InputStream in = req.getInputStream();
	                     if (in == null) {
	                         throw new IOException("Missing content in the request");
	                     }
	                     return req.getInputStream();
	                 }
	             };
	         }
	
	public void guardarArchivo() throws ServletException, IOException {
		if (!ServletFileUpload.isMultipartContent(getServletRequest())) {
			throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");
		}
		
		PrintWriter writer = getServletResponse().getWriter();
		getServletResponse().setContentType("text/plain");
		String contextPath = getServletRequest().getSession().getServletContext().getRealPath("/")+"imagenes\\"+uploadFileName;
		String fullFileName = "C:\\Users\\K\\Desktop\\";
		String tamanio ="22";
		HttpServletRequest request = getServletRequest();
		File theFile = new File(contextPath);
		writer.write("{\"name\":\""+ uploadFileName + "\",\"type\":\"" + uploadContentType+ "\",\"size\":\"" + tamanio + "\"}");
		System.out.println(contextPath);
		FileUtils.copyFile(upload, theFile);
		
		
//		ServletFileUpload uploadHandler = new ServletFileUpload(new DiskFileItemFactory());
//		PrintWriter writer = getServletResponse().getWriter();
//		getServletResponse().setContentType("text/plain");
//		
//		try {
//			List<FileItem> items = uploadHandler.parseRequest(createRequestContext(request));
//			for (FileItem item : items) {
//				if (!item.isFormField()) {
//					File file = new File("C:\\Users\\K\\Desktop", item.getName());//File.createTempFile(item.getName(), "");
//					item.write(file);
//					writer.write("{\"name\":\""+ item.getName() + "\",\"type\":\"" + item.getContentType() + "\",\"size\":\"" + item.getSize() + "\"}");
//					break; // assume we only get one file at a time
//				}
//			}
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		} finally {
//			writer.close();
//		}

	}
}
